{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 创建数据库具体步骤\n",
    "\n",
    "\n",
    "# 1.大概：\n",
    "\n",
    "\n",
    "# 描述\n",
    "\n",
    "\n",
    "### 创建一个人脸的集合 FaceSet，用于存储人脸标识 face_token。一个 FaceSet 能够存储10000个 face_token。\n",
    "\n",
    "### 试用API Key可以创建1000个FaceSet，正式API Key可以创建10000个FaceSet。\n",
    "\n",
    "### 调用URL\n",
    "### https://api-cn.faceplusplus.com/facepp/v3/faceset/create\n",
    "\n",
    "### 调用方法\n",
    "### POST"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 创建数据集合\n",
    "* 第一部分"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 准备工作\n",
    "## API_KEY = 'nViWRP71ph0DWl2oEjlKEDn3R-X_2anN'\n",
    "## API_S = 'ilB3hpzmGQB04tHFug5JcObKxDQIPEtK'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 开始创建\n",
    "API_KEY = 'nViWRP71ph0DWl2oEjlKEDn3R-X_2anN'\n",
    "API_S = 'ilB3hpzmGQB04tHFug5JcObKxDQIPEtK'\n",
    "\n",
    "FaceSet_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S\n",
    "}\n",
    "\n",
    "r = requests.post(url = FaceSet_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '67656259690881ac538c6ee1c31f118a',\n",
       " 'time_used': 157,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1647917571,dd5095d5-ca98-401d-8a32-22ecff4d1ff5',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第二部分 人脸数据库的查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 检查数据库\n",
    "API_KEY = 'nViWRP71ph0DWl2oEjlKEDn3R-X_2anN'\n",
    "API_S = 'ilB3hpzmGQB04tHFug5JcObKxDQIPEtK'\n",
    "\n",
    "FaceSet_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets'\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S\n",
    "}\n",
    "\n",
    "r = requests.post(url = FaceSet_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 92,\n",
       " 'facesets': [{'faceset_token': '67656259690881ac538c6ee1c31f118a',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1647918740,8858bf19-ba51-4584-86fc-76411f639b4a'}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "getdetail_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "    'faceset_token':'67656259690881ac538c6ee1c31f118a'\n",
    "}\n",
    "r = requests.post(url = getdetail_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '67656259690881ac538c6ee1c31f118a',\n",
       " 'tags': '',\n",
       " 'time_used': 91,\n",
       " 'user_data': '1人',\n",
       " 'display_name': 'zzh的库',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1647919244,83199e89-cbaa-41b7-ba53-f25a272fc008',\n",
       " 'outer_id': 'API2010130027'}"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 更新人脸库信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第三步"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "API_KEY = 'nViWRP71ph0DWl2oEjlKEDn3R-X_2anN'\n",
    "API_S = 'ilB3hpzmGQB04tHFug5JcObKxDQIPEtK'\n",
    "update_FaceSet_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S,\n",
    "    'faceset_token':'67656259690881ac538c6ee1c31f118a',\n",
    "    'new_outer_id':'API2',\n",
    "    'display_name':'zzh的库',\n",
    "    'user_data':'1人',\n",
    "    'tags':''\n",
    "}\n",
    "r = requests.post(url = update_FaceSet_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '67656259690881ac538c6ee1c31f118a',\n",
       " 'request_id': '1647920095,e4b8340e-a94f-40a8-a990-af3722ac1a8a',\n",
       " 'time_used': 52,\n",
       " 'outer_id': 'API2'}"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 第四步 小tips"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 获取所有的数据库集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "API_KEY = 'nViWRP71ph0DWl2oEjlKEDn3R-X_2anN',\n",
    "API_S = 'ilB3hpzmGQB04tHFug5JcObKxDQIPEtK',\n",
    "FaceSet_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets'\n",
    "payload = {\n",
    "    'api_key':API_KEY,\n",
    "    'api_secret':API_S\n",
    "}\n",
    "\n",
    "r = requests.post(url = FaceSet_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 79,\n",
       " 'facesets': [{'faceset_token': '67656259690881ac538c6ee1c31f118a',\n",
       "   'outer_id': 'API2',\n",
       "   'display_name': 'zzh的库',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1647920279,6ff2c65f-bfd2-4ef5-a6e2-8cca14123642'}"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "detect.face_detect(API_KEY,API_S,'# 放照片')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# detect.py实现并调用\n",
    "# analyze.py/campare.py/search.py，并实现功能调用\n",
    "# 尝试放4位人脸信息\n",
    "# 完善faceset信息\n",
    "# 实现人脸搜索和人脸对比，打印结果：是本人or不是本人"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
